﻿using SVS_S_SDK_COMLib;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace CAPlugin.Interface
{
    public interface IServer
    {
        /// <summary>
        /// 初始化服务器
        /// </summary>
        /// <param name="serverIP">IP</param>
        /// <param name="serverPort">端口</param>
        /// <param name="serverUri">uri</param>
        /// <param name="timeOut">链接超时 -1：永不超时</param>
        /// <returns>0成功，其他失败</returns>
        int SetServerInfo(string serverIP, int serverPort, string serverUri, int timeOut);

        /// <summary>
        /// 设置摘要算法
        /// </summary>
        /// <param name="hashMethod">“MD5”，“SHA1”，“SHA256”，“SM3”</param>
        /// <returns>0成功，其他失败</returns>
        int SetHashMethod(string hashMethod);

        /// <summary>
        /// 获取hash算法
        /// </summary>
        /// <returns></returns>
        string GetHashMethod();

        /// <summary>
        /// 设置对称加密算法
        /// </summary>
        /// <param name="encryptMethod"></param>
        /// <returns>0成功，其他失败</returns>
        int SetEncryptMethod(string encryptMethod);

        /// <summary>
        /// 获取对称加密算法
        /// </summary>
        /// <returns></returns>
        string SOF_GetEncryptMethod();

        /// <summary>
        /// 获取指定位数随机数
        /// </summary>
        /// <param name="length">位数</param>
        /// <returns></returns>
        string GenRandom(short length);

        /// <summary>
        /// 获取证书信息
        /// </summary>
        /// <param name="certAlias">证书标识</param>
        /// <param name="sType">1：证书拥有者DN信息、2：证书标识、3：证书颁发者DN信息、13：证书实体base64</param>
        /// <returns>证书信息</returns>
        string GetCertInfo(string certAlias, short sType);

        /// <summary>
        /// 获得证书扩展信息
        /// 通过证书别名和属性oid获取证书的相应信息
        /// 备注：该函数暂时不可用
        /// </summary>
        /// <param name="certAlias">证书标识</param>
        /// <param name="oid">证书中属性的oid</param>
        /// <returns></returns>
        string GetCertInfoByOid(string certAlias, string oid);

        /// <summary>
        /// 验证证书有效性
        /// </summary>
        /// <param name="certAlias">证书标识</param>
        /// <returns>0成功，其他失败</returns>
        int ValidateCert(string certAlias);

        /// <summary>
        /// P1数据签名
        /// </summary>
        /// <param name="inData">待签名数据</param>
        /// <param name="inDataLength">代签名数据长度</param>
        /// <returns>签名值</returns>
        string SignData(string inData, int inDataLength);

        /// <summary>
        /// P1验签
        /// </summary>
        /// <param name="certAlias">证书标识</param>
        /// <param name="inData"></param>
        /// <param name="inDataLength"></param>
        /// <param name="signValue"></param>
        /// <returns>0成功，其他失败</returns>
        int VerifySignedData(string certAlias, string inData, int inDataLength, string signValue);

        /// <summary>
        /// P1文件签名
        /// </summary>
        /// <param name="inFile">代签名文件</param>
        /// <returns>文件签名值</returns>
        string SignFile(string inFile);

        /// <summary>
        /// P1文件验签
        /// </summary>
        /// <param name="certAlias">证书标识</param>
        /// <param name="inFile">待验文件</param>
        /// <param name="signValue">签名值</param>
        /// <returns></returns>
        int VerifySignedFile(string certAlias, string inFile, string signValue);

        /// <summary>
        /// 数据对称加密
        /// </summary>
        /// <param name="certAlias">证书标识</param>
        /// <param name="inData"></param>
        /// <returns>加密后密文</returns>
        string EncryptData(string certAlias, string inData);

        /// <summary>
        /// 数据对称解密
        /// </summary>
        /// <param name="certAlias">证书标识</param>
        /// <param name="inData">待解密数据</param>
        /// <returns>解密后数据</returns>
        string DecryptData(string certAlias, string inData);

        /// <summary>
        /// 文件内容对称加密后输出到指定文件
        /// </summary>
        /// <param name="certAlias">证书标识</param>
        /// <param name="inFile">待加密文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns>0成功，其他失败</returns>
        int EncryptFile(string certAlias, string inFile, string outFile);

        /// <summary>
        /// 文件对称解密后输出到指定文件
        /// </summary>
        /// <param name="certAlias">证书标识</param>
        /// <param name="inFile">待加密文件</param>
        /// <param name="outFile">输出文件</param>
        /// <returns>0成功，其他失败</returns>
        int DecryptFile(string certAlias, string inFile, string outFile);

        /// <summary>
        /// 使用公钥加密数据
        /// </summary>
        /// <param name="certAlias">证书标识</param>
        /// <param name="inData">待加密数据</param>
        /// <returns></returns>
        string PubKeyEncrypt(string certAlias, string inData);

        /// <summary>
        /// 私钥解密
        /// </summary>
        /// <param name="certAlias">证书标识</param>
        /// <param name="inData">待解密数据</param>
        /// <returns>解密后数据</returns>
        string PriKeyDecrypt(string certAlias, string inData);

        /// <summary>
        /// P7数据签名
        /// </summary>
        /// <param name="inData">待签名数据</param>
        /// <returns>0表示成功，1表示失败</returns>
        string SignDataPkcs7(string inData);

        /// <summary>
        /// P7数据验签
        /// </summary>
        /// <param name="inData"></param>
        /// <returns>0表示成功，1表示失败</returns>
        string VerifySignedDataPkcs7(string inData);

        /// <summary>
        /// 获取P7签名信息
        /// </summary>
        /// <param name="inData"></param>
        /// <param name="sType">0：原文信息、1：颁发者信息、2：标识、3：P1签名数据、4：签名证书</param>
        /// <returns>签名信息</returns>
        string GetP7SignDataInfo(string inData, short sType);

        /// <summary>
        /// XML数据签名
        /// </summary>
        /// <param name="inData">待签名XML数据</param>
        /// <returns></returns>
        string SignDataXML(string inData);

        /// <summary>
        /// XML验签
        /// </summary>
        /// <param name="inData">签名结果</param>
        /// <returns>0成功，1失败</returns>
        int VerifySignedDataXML(string inData);

        /// <summary>
        /// 创建签名时间戳
        /// </summary>
        /// <param name="inData"></param>
        /// <returns>时间戳</returns>
        string CreateTimeStampResponse(string inData);

        /// <summary>
        /// 验证时间戳
        /// </summary>
        /// <param name="inData">时间戳结果</param>
        /// <returns>时间戳时间</returns>
        string VerifyTimeStamp(string inData);

        /// <summary>
        /// 获取时间戳信息
        /// </summary>
        /// <param name="tsResponseData"></param>
        /// <param name="sType"></param>
        /// <returns></returns>
        string GetTimeStampInfo(string tsResponseData, short sType);

        /// <summary>
        /// 设置P7签名模式
        /// </summary>
        /// <param name="sModel">0：attach模式；1：detach模式</param>
        /// <returns>0表示成功，1表示失败</returns>
        int SetP7SignMode(short sModel);

        /// <summary>
        /// 设置P1验签模式
        /// </summary>
        /// <param name="sMode">0：attach模式；1：detach模式</param>
        /// <returns>0成功，1失败</returns>
        int SetP1VerifyMode(short sMode);

        /// <summary>
        /// 设置使用证书的标记
        /// </summary>
        /// <param name="signedCertAlias">签名的证书标识</param>
        /// <param name="encryptedCertAlias">加密后的证书标识</param>
        /// <returns>0成功，1失败</returns>
        int SetCertAlias(string signedCertAlias, string encryptedCertAlias);

        /// <summary>
        /// 使用公钥加密文件
        /// </summary>
        /// <param name="certAlias">证书序列号</param>
        /// <param name="inFile">待加密文件</param>
        /// <param name="outFile">加密后输出文件</param>
        /// <returns></returns>
        int PubKeyEncryptFromFile(string certAlias, string inFile, string outFile);


        /// <summary>
        /// 使用私钥解密文件
        /// </summary>
        /// <param name="certAlias">证书序列号</param>
        /// <param name="inFile">待解密文件</param>
        /// <param name="outFile">解密后输出文件</param>
        /// <returns></returns>
        int PriKeyDecryptFromFile(string certAlias, string inFile, string outFile);


        /// <summary>
        /// Detach签名汇总
        /// </summary>
        /// <param name="inData">签名结果呢</param>
        /// <param name="srcData">原文数据</param>
        /// <returns></returns>
        int VerifyDetachSignedData(string inData, string srcData);


        /// <summary>
        /// P7文件签名
        /// </summary>
        /// <param name="certAlias">证书序列号</param>
        /// <param name="inFile">待签名文件</param>
        /// <param name="outFile">签名值输出文件</param>
        /// <returns>0成功，1失败</returns>
        int P7SignFromFile(string certAlias, string inFile, string outFile);


        /// <summary>
        /// P7文件验签
        /// </summary>
        /// <param name="inFile"></param>
        /// <param name="outFile"></param>
        /// <returns>0成功，1失败</returns>
        int P7VerifyFromFile(string inFile, string outFile);

        /// <summary>
        /// 将数据加密成数字信封
        /// </summary>
        /// <param name="certAlias">证书序列号</param>
        /// <param name="inData">待加密数据</param>
        /// <returns>数字信封内容</returns>
        string EncryptEnvelope(string certAlias, string inData);

        /// <summary>
        /// 信封解密
        /// </summary>
        /// <param name="inData">待加密数据</param>
        /// <returns>解密后数据</returns>
        string DecryptEnvelope(string inData);


        /// <summary>
        /// 对文件加密数字信封
        /// </summary>
        /// <param name="certAlias">证书序列号</param>
        /// <param name="inFile">待加密文件</param>
        /// <param name="outFile">加密后输出文件</param>
        /// <returns>0成功，1失败</returns>
        int EncryptEnvelopeFromFile(string certAlias, string inFile, string outFile);

        /// <summary>
        /// 从文件解密数字信封
        /// </summary>
        /// <param name="inFile">待解密文件</param>
        /// <param name="outFile">解密结果输出文件</param>
        /// <returns>0成功，1失败</returns>
        int DecryptEnvelopeFromFile(string inFile, string outFile);

        /// <summary>
        /// 将数据加密成签名后的数字信封
        /// </summary>
        /// <param name="signedCertAlias">签名后的证书序列号</param>
        /// <param name="encryptedCertAlias">加密后的证书序列号</param>
        /// <param name="inFile">待加密文件</param>
        /// <param name="outFile"></param>
        /// <returns></returns>
        string EncryptSignedEnvelope(string signedCertAlias, string encryptedCertAlias, string inData);

        /// <summary>
        /// 解密签名数字信封
        /// </summary>
        /// <param name="inData">签名数字信封</param>
        /// <returns>数字信封解密后的数据</returns>
        string DecryptSignedEnvelope(string inData);

        /// <summary>
        /// 将文件加密为数字信封
        /// </summary>
        /// <param name="signedCertAlias">证书序列号签名值</param>
        /// <param name="encyptedCertAlias">证书序列号对称加密</param>
        /// <param name="inFile">待加密文件</param>
        /// <param name="outFile">机密后输出文件</param>
        /// <returns>0、成功 1、失败</returns>
        int EncryptSignedEnvelopeFromFile(string signedCertAlias, string encyptedCertAlias, string inFile, string outFile);

        /// <summary>
        /// 从文件解密签名电子信封，到指定文件
        /// </summary>
        /// <param name="inFile"></param>
        /// <param name="outFile"></param>
        /// <returns></returns>
        int DecryptSignedEnvelopeFromFile(string inFile, string outFile);

        /// <summary>
        /// 对数据进行Base64编码
        /// </summary>
        /// <param name="inData">待编码数据</param>
        /// <returns>编码结果</returns>
        string Base64Encode(string inData);

        /// <summary>
        /// 设置时间戳签名模式
        /// </summary>
        /// <param name="sMode">时间戳模式，0：2161；1：3610</param>
        /// <returns>0、成功 1、失败</returns>
        int SetTSASignMode(short sMode);


        /// <summary>
        /// 获取错误信息
        /// </summary>
        /// <returns>错误信息</returns>
        string GetErrorMsg();

        /// <summary>
        /// 解析时间戳
        /// </summary>
        /// <param name="tsId">时间戳ID</param>
        /// <returns>时间戳签名值</returns>
        string GetTimeStampData(string tsId);

        /// <summary>
        /// 服务器初始化
        /// </summary>
        /// <returns></returns>
        bool ServerInit(out string errMsg);

        /// <summary>
        /// 时间戳Sdk
        /// </summary>
        CSVS_S_SDK TssSDK { get; }
    }

}
